/**
 * 
 */
package com.easidea.tim.service.system;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.system.GeographyDao;
import com.easidea.tim.entity.system.Geography;

/**
 * @author Harrison
 *
 */
public class GeographyService extends EntityService<Geography, GeographyDao> {
	
	private GeographyDao geographyDao;
	private final Logger logger = Logger.getLogger(GeographyService.class);
	
	/**
	 * 
	 * @param parentGeoID
	 * @return
	 */
	public List<Geography> listAreas(Long parentGeoID){
		List<Geography> areaList = null;
		Criteria cte = geographyDao.createUnDeletedCriteria();
		
		if(parentGeoID != null){
			cte.add(Restrictions.eq("parentGeo", parentGeoID));
		} else {
			cte.add(Restrictions.isNull("parentGeo"));
		}
		areaList = (List<Geography>)cte.list();
		return areaList;
	}
	
	
	/**
	 * 保存数据
	 * @param geo
	 * @return <code>0</code> 参数错误，空值
	 * 			<code>-2</code>保存修改的数据失败，无对应父对象
	 * 			<code>1</code>保存新的一级数据成功（不区分是一级还是二级）
	 * 			<code>2</code>保存修改的数据成功 
	 */
	public String saveGeo(Geography geo){
		if(geo != null){
			if(geo.getId() != null && geo.getId() != -1) {
				// update old geo data
				Geography oldGeo = geographyDao.get(geo.getId());
				if(oldGeo != null){
					if(geo.getName() != null) oldGeo.setName(geo.getName().trim());
					if(geo.getDeleted() != null)oldGeo.setDeleted(geo.getDeleted());
					if(geo.getModifier() != null)oldGeo.setModifier(geo.getModifier().trim());
					if(geo.getModifyTime() != null)oldGeo.setModifyTime(geo.getModifyTime());
					if(geo.getParentGeo() != null)oldGeo.setParentGeo(geo.getParentGeo());
					geographyDao.save(oldGeo);
					logger.debug("update geo : " + geo.getName() + ", has parent ? " + geo.getParentGeo() == null ? true:false);
					return "2";
				}else{
					logger.debug("update geo : " + geo.getName() + " failure ~~! caused by not found the data with id: "+geo.getId());
					return "-2";
				}
			}else{
				// add new geo data
				geo.setId(null); // fit for flex remote object conversions with int default value 0
				geographyDao.save(geo);
				logger.debug("save geo : " + geo.getName() + ", new Id : " + geo.getId());
				return (geo.getParentGeo() != null ? "11:" : "1:")+geo.getId();
			}
		}else{
			logger.error("save geo error with null object !");
			return "0";
		}
	}
	
	
	/**
	 * 删除地区信息，需要判断当前数据是否存在，同时判断其是否有子对象存在
	 * 
	 * @param geo
	 * @return <code>0</code> 成功 <code>-1</code>空值 <code>-2</code>指定ID找不到数据 <code>-3</code>有子对象
	 */
	public short delGeo(Geography geo) {
		if(geo != null && geo.getId() != null) {
			Geography oldGeo = geographyDao.get(geo.getId());
			if(oldGeo != null){
				if(oldGeo.getSonList() != null && oldGeo.getSonList().size() > 0){
					logger.debug("delete geography with id: "+oldGeo.getId()+" and name : "+ oldGeo.getName()+" failure, caused by it has son lists ~!");
					return -3; // 有子对象
				}else{
					short s = 1;
					oldGeo.setDeleted(s);
					if(geo.getModifier() != null)oldGeo.setModifier(geo.getModifier());
					oldGeo.setModifyTime(new Date());
					logger.debug("delete geography with id: "+oldGeo.getId()+" and name : "+ oldGeo.getName()+" success ~!");
					return 0;
				}
			}else{
				logger.debug("delete geography with id: "+oldGeo.getId()+" failure, caused by the data with assigned id does not exists ~!");
				return -2; // 无对应数据
			}
		}else{
			logger.debug("can not delete null object with geography ~!");
			return -1;
		}
	}
//	/**
//	 * 判断是否可以删除
//	 * 
//	 * @param gid
//	 * @return
//	 */
//	public boolean canRemoveGeo(Long gid) {
//		Criteria cte = geographyDao.createUnDeletedCriteria();
//		cte.add(Restrictions.eq("parentGeo", gid));
//		List list = cte.list();
//		return (list != null && list.size() > 0) ? false:true;
//	}

	/* (non-Javadoc)
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public GeographyDao getEntityDao() {
		return geographyDao;
	}

	/**
	 * @return the geographyDao
	 */
	public GeographyDao getGeographyDao() {
		return geographyDao;
	}

	/**
	 * @param geographyDao the geographyDao to set
	 */
	public void setGeographyDao(GeographyDao geographyDao) {
		this.geographyDao = geographyDao;
	}
	
}
